//
//  IncomeController.m
//  MoneyIndividual
//
//  Created by Tai Le on 12/30/13.
//  Copyright (c) 2013 Le Anh Tai. All rights reserved.
//

#import "IncomeController.h"
#import "TitleTextFieldCell.h"
#import "TitleNumberCell.h"
#import "TitleIncomeTypeCell.h"
#import "TitleCatalogCell.h"
#import "TitleDateCell.h"
#import "TitleRepeatCell.h"
#import "TitleTextViewCell.h"

@interface IncomeController ()

@end

@implementation IncomeController

#pragma mark - Lifecycle
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _rows = [[NSMutableArray alloc] init];
    
    _accountCell = [self newCellWithTitle:NSLocalizedString(@"Loại", nil) placeHolder:@"" class:[TitleIncomeTypeCell class] key:ACCOUNT_ENITY];
    
    [self generateRowsForType:TransactionTypeIncomeMain managedObject:[[CoreData shared] newEntity:TRANSACTION_ENTITY]];
    
    self.title = @"Thu";
}

#pragma mark - Logic functions
- (void)generateRowsForType:(TransactionType)type managedObject:(NSManagedObject*)managedObject
{
    [_rows removeAllObjects];
    _managedObject = managedObject;
    
    _accountCell.selectedValue = [[CoreData shared] getAccountByType:type];
    
    if (type == TransactionTypeIncomeMain)
    {
        [self generateRowsForIncomeMain];
    }
    else if (type == TransactionTypeIncomeLoan)
    {
        [self generateRowsForIncomeLoan];
    }
    else if (type == TransactionTypeIncomeDebt)
    {
        [self generateRowsForIncomeDebt];
    }
    else if (type == TransactionTypeIncomeHui)
    {
        [self generateRowsForIncomeHui];
    }
    
    [self loadCatalogDataByAccount];
    
    [self.tableView reloadData];
}

- (id)getCellByKey:(NSString*)key
{
    for (TitleTextFieldCell*cell in _rows)
    {
        if ([cell.key isEqualToString:key])
        {
            return cell;
        }
    }
    
    return nil;
}

- (void)generateRowsForIncomeMain
{
    [_rows addObject:_accountCell];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Danh mục", nil)
                                placeHolder:NSLocalizedString(@"Chọn doanh mục", nil)
                                      class:[TitleCatalogCell class]
                                        key:CATALOG_KEY]];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Số tiền", nil)
                                placeHolder:NSLocalizedString(@"Số tiền", nil)
                                      class:[TitleNumberCell class]
                                        key:PRICE_KEY]];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Ngày", nil)
                                placeHolder:@""
                                      class:[TitleDateCell class]
                                        key:CREATED_DATE_KEY]];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Lặp lại", nil)
                                placeHolder:@""
                                      class:[TitleRepeatCell class]
                                        key:TRANSACTION_TEMP_KEY]];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Ghi chú", nil)
                                placeHolder:@""
                                      class:[TitleTextViewCell class]
                                        key:NOTE_KEY]];
}

- (void)generateRowsForIncomeLoan
{
    [_rows addObject:_accountCell];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Người trả", nil)
                                placeHolder:NSLocalizedString(@"Chọn", nil)
                                      class:[TitleCatalogCell class]
                                        key:CATALOG_KEY]];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Số tiền trả", nil)
                                placeHolder:NSLocalizedString(@"Số tiền", nil)
                                      class:[TitleNumberCell class]
                                        key:PRICE_KEY]];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Nợ gốc trả", nil)
                                placeHolder:NSLocalizedString(@"Số tiền", nil)
                                      class:[TitleNumberCell class]
                                        key:ROOT_PRICE_KEY]];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Ngày", nil)
                                placeHolder:@""
                                      class:[TitleDateCell class]
                                        key:CREATED_DATE_KEY]];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Lặp lại", nil)
                                placeHolder:@""
                                      class:[TitleRepeatCell class]
                                        key:TRANSACTION_TEMP_KEY]];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Ghi chú", nil)
                                placeHolder:@""
                                      class:[TitleTextViewCell class]
                                        key:NOTE_KEY]];
}

- (void)generateRowsForIncomeDebt
{
    [_rows addObject:_accountCell];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Vay của", nil)
                                placeHolder:NSLocalizedString(@"chọn", nil)
                                      class:[TitleCatalogCell class]
                                        key:CATALOG_KEY]];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Số tiền", nil)
                                placeHolder:NSLocalizedString(@"Số tiền", nil)
                                      class:[TitleNumberCell class]
                                        key:PRICE_KEY]];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Ngày", nil)
                                placeHolder:@""
                                      class:[TitleDateCell class]
                                        key:CREATED_DATE_KEY]];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Lặp lại", nil)
                                placeHolder:@""
                                      class:[TitleRepeatCell class]
                                        key:TRANSACTION_TEMP_KEY]];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Ghi chú", nil)
                                placeHolder:@""
                                      class:[TitleTextViewCell class]
                                        key:NOTE_KEY]];
}

- (void)generateRowsForIncomeHui
{
    [_rows addObject:_accountCell];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Dây hụi", nil)
                                placeHolder:NSLocalizedString(@"chọn", nil)
                                      class:[TitleCatalogCell class]
                                        key:CATALOG_KEY]];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Số tiền hốt", nil)
                                placeHolder:NSLocalizedString(@"Đã trừ hoa hồng", nil)
                                      class:[TitleNumberCell class]
                                        key:PRICE_KEY]];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Ngày", nil)
                                placeHolder:@""
                                      class:[TitleDateCell class]
                                        key:CREATED_DATE_KEY]];
    [_rows addObject:[self newCellWithTitle:NSLocalizedString(@"Ghi chú", nil)
                                placeHolder:@""
                                      class:[TitleTextViewCell class]
                                        key:NOTE_KEY]];
}

- (TitleTextFieldCell*)newCellWithTitle:(NSString*)title placeHolder:(NSString*)playHolder class:(Class)class key:(NSString*)key
{
    NSString *cellIdentifier = [class description];
    TitleTextFieldCell *cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil)
    {
        cell = [[class alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    cell.key = key;
    cell.titleLabel.text = title;
    cell.textField.placeholder = playHolder;
    cell.delegate = self;
    
    return cell;
}

- (void)loadCatalogDataByAccount
{
    NSManagedObject *selectedAccount = _accountCell.selectedValue;
    NSSet *catalogs = [selectedAccount valueForKeyEx:CATALOG_KEY];
    
    TitleCatalogCell *cell = [self getCellByKey:CATALOG_KEY];
    cell.catalogs = [catalogs allObjects];
}

#pragma mark - Delegate UITableView
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
    return _rows.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TitleTextFieldCell *cell = [_rows objectAtIndex:indexPath.row];
    if ([cell.key isEqualToString:NOTE_KEY])
    {
        return HEI_CELL_NOTE;
    }
    
    return HEI_CELL_DEFAULT;
}

- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    TitleTextFieldCell *cell = [_rows objectAtIndex:indexPath.row];
    if (_managedObject && cell.key && [cell.key isEqualToString:ACCOUNT_ENITY] == NO)
    {
        cell.selectedValue = [_managedObject valueForKeyEx:cell.key];
    }
    
    return cell;
}

#pragma mark - cell delegate
- (void)textFieldCellDidSelectValue:(TitleTextFieldCell *)cell
{
    if ([cell.key isEqualToString:ACCOUNT_ENITY])
    {
        NSManagedObject *selectedAccount = cell.selectedValue;
        TransactionType type = [[selectedAccount valueForKeyEx:TYPE_KEY] integerValue];

        [self generateRowsForType:type managedObject:_managedObject];
        
        TitleCatalogCell *catalogCell = [self getCellByKey:CATALOG_KEY];
        [catalogCell.textField becomeFirstResponderAfterDelay];
    }
    else
    {
        if ([cell.key isEqualToString:CATALOG_KEY])
        {
            TitleCatalogCell *cell = [self getCellByKey:PRICE_KEY];
            [cell.textField becomeFirstResponderAfterDelay];
        }
        
        [_managedObject setValueEx:cell.selectedValue forKey:cell.key];
    }
}

@end
